package com.lzq.badminton.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzq.badminton.Exception.SystemException;
import com.lzq.badminton.dto.BookingDto;
import com.lzq.badminton.dto.UpdateBookingDto;
import com.lzq.badminton.entity.Booking;
import com.lzq.badminton.enums.AppHttpCodeEnum;
import com.lzq.badminton.mapper.BookingMapper;
import com.lzq.badminton.service.BookingService;
import com.lzq.badminton.utils.BeanCopyUtils;
import com.lzq.badminton.vo.BookingVo;
import com.lzq.badminton.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class BookingServiceImpl extends ServiceImpl<BookingMapper, Booking> implements BookingService {

    @Autowired
    private BookingMapper bookingMapper;


    @Override
    public void addBookings(List<Booking> bookingList) {
//        List<Booking> bookings = BeanCopyUtils.copyBeanList(bookingDtoList, Booking.class);
        for(Booking booking : bookingList){
            System.out.println("hello");
            System.out.println(booking.toString());
            if(!save(booking)){
                throw new SystemException(AppHttpCodeEnum.INSERT_ERROR);
            }
        }
    }

    @Override
    public List<BookingVo> listBookings(Long arenaId, LocalDate localDate) {
        LambdaQueryWrapper<Booking> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Booking::getArenaId, arenaId);
        lambdaQueryWrapper.eq(Booking::getDate, localDate);
        List<Booking> bookingList = list(lambdaQueryWrapper);
        List<BookingVo> bookingVos = BeanCopyUtils.copyBeanList(bookingList, BookingVo.class);
        return bookingVos;

    }

    @Override
    public ResponseResult updateBookings(List<UpdateBookingDto> updateBookingDtoList) {
        updateBookingDtoList.stream().
                forEach(updateBookingDto -> {
                    bookingMapper.updateStatus(updateBookingDto.getBookingId(), updateBookingDto.getStatus());
                });
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult orderBookings(List<UpdateBookingDto> updateBookingDtoList) {
        int i = 0;
        for(UpdateBookingDto updateBookingDto : updateBookingDtoList) {
            i = bookingMapper.orderBooking(updateBookingDto);
            System.out.println("i: " + i);
            if(i == 0){
                throw new SystemException(AppHttpCodeEnum.BOOKING_ERROR);
            }
        }
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult removeBooking(Long bookingId) {
        if(!removeById(bookingId)){
            return ResponseResult.errorResult(AppHttpCodeEnum.REMOVE_ERROR);
        }
        return ResponseResult.okResult();
    }

    @Override
    public List<BookingVo> listByOrderId(Long orderId) {
        LambdaQueryWrapper<Booking> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Booking::getOrderId, orderId);
        List<Booking> bookingList = list(lambdaQueryWrapper);
        List<BookingVo> bookingVoList = BeanCopyUtils.copyBeanList(bookingList, BookingVo.class);
        return bookingVoList;
    }

    @Override
    public void clearOrderId(Long orderid) {
        bookingMapper.clearOrderId(orderid);
    }

    @Override
    public int deleteRecord() {
        return baseMapper.delete(null);
    }
}